<?php
namespace app\admin\controller;
use think\Db;

class Linkcat extends Base
{   
    public function lists(){
        if ( is_post() ) {
    		$page     = input('page');
            $status   = input('status');
            $keyword  = input('keyword');
            $min_time = input('min_time');
            $max_time = input('max_time');
            //原生态sql查询
            $sql = "
                select
                    [1]
                from
                    link_cat
                where 
                    1 = ?
                ";
            $param = [1];
            if ( $status != '100all' ) {
                $sql .= " and status = ?";
                $param[] = $status;
            }
            if ( !empty($keyword) ) {
                $sql .= " and cat_title like ?";
                $param[] = "%{$keyword}%";
            }
            if ( !empty($min_time) ) {
                $sql    .= " and create_time >= ?";
                $param[] = strtotime($min_time);
            }
            if ( !empty($max_time) ) {
                $sql    .= " and create_time <= ?";
                $param[] = strtotime($max_time);
            }
            //分页
            $sql_count = str_replace("[1]", "count(*) as total", $sql);
            $total     = Db::query($sql_count, $param);
            $total     = $total[0]['total'];
            $page_size   = c('page_size');
            $page        = com_cur_page($total, $page,  $page_size);
			$page_data   = com_page_data($total, $page,  $page_size);
            
            //列表
			$start       = ($page - 1) * $page_size;
			$sql        .= " order by sort desc,id desc limit $start, $page_size";
			$field       = "*";
			$sql_list    = str_replace("[1]", $field, $sql);
            $list        = Db::query($sql_list, $param);
            //格式数据
            foreach($list as $k => $v){                     
                $list[$k]['checkbox']       =  com_format_checkbox($v['id']);
                $list[$k]['create_time']    =  com_format_time( $v['create_time'] );

                $list[$k]['sort']           =  com_page( ['url'=>url("c/edit"),'val'=>$v['sort'],'post'=>['t'=>'link_cat','id'=>$v['id'],'f'=>'sort']],'input' );
                $list[$k]['status']         =  com_page( ['url'=>url("c/edit"),'val'=>$v['status'],'post'=>['t'=>'link_cat','id'=>$v['id'],'f'=>'status']],'iphone' );

                //操作按钮
                $list[$k]['action']         =  com_page(['title'=>'编辑分类', 'url'=>url("Linkcat/edit","id={$v['id']}"), 'ico'=>'edit','width'=>'400','height'=>'300'] );
                $list[$k]['action']        .=  com_page(['title'=>'拷贝分类','url'=>url("c/copy"),'post'=>['t'=>'link_cat','id'=>$v['id']],'ico'=>'copy']);
                $list[$k]['action']        .=  com_page(['title'=>'删除', 'url'=>url("Linkcat/delete"), 'ico'=>'del3','post'=>['id'=>$v['id']],'confirm'=>"你确定要删除[{$v['cat_title']}]吗？",] );
            }

			$com_body    = com_build_table( $list, array("checkbox", "id",  "cat_title","sort", "status","create_time","action") );
			$com_page    = com_build_page( $page_data );
			$data = array(
				"com_body"  => $com_body,
				"com_page"  => $com_page,
			);
			com_check(['data'=>$data,'suc'=>['js_action'=>'function.iphone']]);
    	}
        $data = [
            'status'    => ['100all'=>'全部状态'] + c('type.status'),
            'nav'       => ['友情链接分类', '列表'],
        ];
        return view('', $data);
    }
    
    //添加
    public function add(){
        if( is_post() ){
            $data = com_data(input());
            $rule = [
                'cat_title'      =>  'require',
            ];
            $msg = [
                'cat_title.require'   =>  '标题不能为空',
            ];
            com_validate( $rule, $msg, $data, true );
            $data['create_time'] = time();
            Db::name('link_cat')->insert($data);
            $config = [
                'suc' => [
                    'msg' => '添加成功！',
                    'js_action' => 'alert|close',
                ],
            ];
            com_check( $config );
        }    
        //模板赋值
        $data = [
            'status'    => c('type.status'),
        ];
        return view('', $data);
    }
    
    
    //编辑
    public function edit( $id ){
        if( is_post() ){
            $data = com_data(input());
            $rule = [
                   'cat_title'     =>  'require',
                ];
            $msg = [
                   'cat_title.require'   =>  '标题不能为空',
                ];
            com_validate( $rule, $msg, $data, true );

            $data['update_time'] = time();
            $data['id'] = $id;
            Db::name('link_cat')->update($data);
            $config = [
                'suc' => [
                    'msg' => '编辑成功！',
                    'js_action' => 'alert|close',
                ],
            ];
            com_check( $config );
        }
        $data = [
            'status'    => c('type.status'),
            'info'      => Db::name('link_cat')->find($id),
        ];
        return view('', $data);
    }
    
    //删除
    public function delete(){
        if( is_post() ){
            $id = input('id');
            $sc =  new \app\admin\model\LinkCat();
            if( $sc->checkDel($id) )
                Db::name('link_cat')->delete( $id );
            
            $config = [
                'suc' => [
                    'msg' => '删除成功！',
                    'js_action'=>"alert|reload",
                ],
            ];
            com_check( $config );
        }
    }
    
    //批量删除
    public function delall(){
        if( is_post() ){
            $ids = input('ids/a');
            $sc =  new \app\admin\model\LinkCat();
            foreach($ids as $id){
                if( $sc->checkDel($id) )
                    Db::name('link_cat')->delete( $id );
            }
            $config = [
                'suc' => [
                    'msg' => '删除成功！',
                    'js_action' => "alert|reload",
                ],
            ];
            com_check($config);
        }
    }

}